<?php
namespace App\Http\Controllers;

use App\Handler\InfoHandler;
use App\Models\Info;
use App\Http\C5\C5ResponseFactory;
use Illuminate\Support\Facades\DB;

class InfoController extends BaseController
{


    public function edit()
    {
        $type_id = $this->get("type_id");
        $organization_id = $this->get("organization_id");
        $content = $this->get("content");
        $result = InfoHandler::saveContent($organization_id, $type_id, $content, true);
        return C5ResponseFactory::newSuccessInstance($result);
    }

    public function create()
    {
        $type_id = $this->get("type_id");
        $content = $this->get("content");
        $organization_id = $this->get("organization_id");
        $result = InfoHandler::saveContent($organization_id, $type_id, $content, true);
        return C5ResponseFactory::newSuccessInstance($result);
    }

    public function detail()
    {
        $info_id = $this->get("info_id");
        return $this->find($info_id);
    }

    public function find($info_id)
    {
        /**
         * @var $bean Info
         */
        $sql = "
            select a.*
            from info a
            where info_id=?
        ";
        $result = DB::select($sql,[$info_id]);
        if(empty($result)){
            return C5ResponseFactory::newRecordNotExistsInstance();
        }
        return C5ResponseFactory::newSuccessInstance($result[0]);
    }

    public function update()
    {
        /**
         * @var  $bean Info
         */
        $type_id = $this->get("type_id");
        $content = $this->get("content");
        $organization_id = $this->get("organization_id");
        $result = InfoHandler::saveContent($organization_id, $type_id, $content, true);
        return C5ResponseFactory::newSuccessInstance($result);
    }

    public function delete()
    {
        /**
         * @var  $bean Info
         */
        $type_id = $this->get("type_id");
        $organization_id = $this->get("organization_id");
        $result = DB::table("info")->where("type_id",$type_id)->where("organization_id",$organization_id)->delete();
        return C5ResponseFactory::newSuccessInstance($result);
    }


}

